import Vue from 'vue'
import App from './App.vue'
import './registerServiceWorker'
import router from './router'
import store from './store'
import '../src/plugins/rem'

import './assets/font/font.css'

import 'vant/lib/index.css';
import 'lib-flexible/flexible'


import FastClick from 'fastclick'
window.addEventListener( "load", function() {
  FastClick.attach( document.body );
}, false );


import 'ant-design-vue/dist/antd.css';
import {Layout,Menu,Carousel,Drawer,BackTop,Avatar} from 'ant-design-vue'
Vue.use(Layout)
Vue.use(Menu)
Vue.use(Carousel)
Vue.use(Drawer)
Vue.use(BackTop)
Vue.use(Avatar)

import moment from 'moment';
import 'moment/locale/zh-cn';
moment.locale('zh-cn');
window.moment=moment

import wx from 'weixin-js-sdk'
window.wx=wx

import { 
  Search,
  Row,
  Overlay,
  NavBar,
  List,
  Icon,
  Toast,
  Button,
  CountDown,
  PasswordInput,
  NumberKeyboard,
  Swipe,
  SwipeItem,
  Picker,
  Area,
  Popup,
  Dialog,
  Tab, Tabs ,
  Sticky,
  Stepper,
  Checkbox, CheckboxGroup,
  Col,
  Cell, CellGroup,
  DatetimePicker,
  Divider,
  Field
 } from 'vant'
Vue.use(Search)
Vue.use(Row)
Vue.use(Overlay)
Vue.use(NavBar)
Vue.use(List)
Vue.use(Icon)
Vue.use(Toast)
Vue.use(Button)
Vue.use(CountDown)
Vue.use(PasswordInput)
Vue.use(NumberKeyboard)
Vue.use(Swipe)
Vue.use(SwipeItem)
Vue.use(Picker)
Vue.use(Area)
Vue.use(Popup)
Vue.use(Dialog)
Vue.use(Tab);
Vue.use(Tabs);
Vue.use(Sticky)
Vue.use(Stepper)
Vue.use(Checkbox)
Vue.use(CheckboxGroup)
Vue.use(Col)
Vue.use(Cell);
Vue.use(CellGroup);
Vue.use(DatetimePicker)
Vue.use(Divider)
Vue.use(Field)


import {getWxCode} from './service/wxService'


//判断手机类型
let UsePhoneType="";
if (/android/i.test(navigator.userAgent)){
　　// 安卓手机
  UsePhoneType="1";
  console.log("安卓手机")
}
if (/ipad|iphone|mac/i.test(navigator.userAgent)){
    //  苹果手机
    UsePhoneType="0";
    console.log("苹果手机")
}
window.UsePhoneType=UsePhoneType


window.addEventListener("popstate", function(e) { 
  console.log("popstate",e)
  // e.preventDefault()
}, false); 


router.beforeEach((to,from,next)=>{
  // console.log("TT",to)
  // console.log("FF",from)
  if(from.path!="/"){//从历史记里获取
    localStorage.setItem("routerHistory",from.path)
  }

  //判断是否需要登录
  if(to.meta.needlogin){
    if(localStorage.getItem("user")==null||localStorage.getItem("user")==undefined){
      router.push("/login")
    }
  }
  // if(localStorage.getItem("user")!=null&&localStorage.getItem("user")!=undefined){

    // let user=localStorage.getItem("user")
    // if(user.MyLoginTime){
    //   let now=new Date().getTime()
    //   if(now-user.MyLoginTime>24*60*60*1000){
    //     localStorage.removeItem("user")
    //   }else{
    //     next()
    //   }
    // }else{
    //   next("/login")
    // }


    // if(to.path=='/login'){
    //   next("/indexs")
    // }
  // }
  //判断WxUserInfo是否存在，如果存在，通过WxCode判断是否过期
  //如果过期，重新获取
  //如果没过期，不管
  //如果不存在，重新获取

  


  // next()

  // WxUserInfo存在，且access_token存在，且没过期
  if(localStorage.getItem("WxUserInfo")!=null&&localStorage.getItem("WxUserInfo")!=undefined){
    if(JSON.parse(localStorage.getItem("WxUserInfo")).access_token!=undefined){
      let logdate=localStorage.getItem("WxCode")
      let thistime=new Date().getTime()
      
      // if(thistime-logdate.time<2*60*60*1000){
        next()
      // }else{
      //   getWxCode(()=>{
      //     next()
      //   })
      // }

    }else{
      getWxCode(()=>{
        next()
      })
    }
  }else{
    getWxCode(()=>{
      next()
    })
  }

  
})


Vue.config.productionTip = false

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
